/*!
    \copyright  Copyright (c) 2020 - 2023 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    le_bap
    \brief      Implementations for the LE BAP Unicast server interfaces.
*/

#include "unicast_server_role.h"
#include "unicast_server_role_advertising.h"
#include "pacs_utilities.h"

#include <gatt_handler_db_if.h>
#include <logging.h>

#ifdef USE_SYNERGY
#include "cm_lib.h"
#include "bap_server_lib.h"
#else
#include "bap_server.h"
#endif
#include "le_bap_profile.h"

#define UNICAST_SERVER_ROLE_LOG     DEBUG_LOG

ServiceHandle bapUnicastServiceHandle = 0;

void LeBapUnicastServer_Init(uint8 number_of_ases, Task bap_handler)
{
    BapServerHandleRange pacsServiceHandle = {HANDLE_PUBLISHED_AUDIO_CAPABILITIES_SERVICE,
                                              HANDLE_PUBLISHED_AUDIO_CAPABILITIES_SERVICE_END};
    BapServerHandleRange ascsServiceHandle = {HANDLE_ASCS_SERVICE, HANDLE_ASCS_SERVICE_END};

    PanicNull((void *) bap_handler);

#ifdef USE_SYNERGY
    bapUnicastServiceHandle = BapServerUnicastInit(TrapToOxygenTask(bap_handler),
                               number_of_ases, &pacsServiceHandle, &ascsServiceHandle);
    UNICAST_SERVER_ROLE_LOG("LeBapUnicastServer_Init bapUnicastServiceHandle=0x%x", bapUnicastServiceHandle);
#else
    bapUnicastServiceHandle = BapServerUnicastInit(bap_handler, number_of_ases,
                                                   &pacsServiceHandle, &ascsServiceHandle);
#endif
    LeBapPacsSetBapHandle(bapUnicastServiceHandle);

    LeBapPacsUtilities_Init();

    LeBapUnicastServer_SetupLeAdvertisingData();
}


void LeBapUnicastServer_CreateDataPath(Task bap_handler, uint16 cis_handle, bool host_to_controller, bool controller_to_host,
                                       bool is_cis_delegated, bool config_ft, bool cis_relay_enabled)
{
    BapServerSetupDataPathReq dataPathParameters = {0};

    dataPathParameters.isoHandle = cis_handle,
    dataPathParameters.dataPathId = is_cis_delegated ? (cis_relay_enabled ? ISOC_DATA_PATH_ID_RAW_STREAM_ENDPOINTS_ONLY : ISOC_DATA_PATH_ID_ISO_PATH_SHADOWING_PERIPHERAL)
                                                     : ISOC_DATA_PATH_ID_RAW_STREAM_ENDPOINTS_ONLY;

    UNICAST_SERVER_ROLE_LOG("LeBapUnicastServer_CreateDataPath: cis_handle=0x%x host_to_controller=%u controller_to_host=%u is_cis_delegated=%u config_ft=%u, cis_relay_enabled=%d, dataPathId=0x%x",
                            cis_handle, host_to_controller, controller_to_host, is_cis_delegated, config_ft, cis_relay_enabled, dataPathParameters.dataPathId);

    if (controller_to_host)
    {
        if(config_ft)
        {
            UNUSED(bap_handler);
        }

        dataPathParameters.dataPathDirection = ISOC_DATA_PATH_DIRECTION_CONTROLLER_TO_HOST;
        BapServerSetupIsoDataPathReq(bapUnicastServiceHandle, BAP_ISO_UNICAST, &dataPathParameters);
    }

    if (host_to_controller)
    {
        dataPathParameters.dataPathId = is_cis_delegated ? ISOC_DATA_PATH_ID_ISO_PATH_SHADOWING_PERIPHERAL
                                                         : ISOC_DATA_PATH_ID_RAW_STREAM_ENDPOINTS_ONLY;

        dataPathParameters.dataPathDirection = ISOC_DATA_PATH_DIRECTION_HOST_TO_CONTROLLER;
        BapServerSetupIsoDataPathReq(bapUnicastServiceHandle, BAP_ISO_UNICAST, &dataPathParameters);
    }
}

void LeBapUnicastServer_RemoveDataPath(uint16 cis_handle, uint8 direction_mask)
{
    UNICAST_SERVER_ROLE_LOG("LeBapUnicastServer_RemoveDataPath: cis_handle=0x%x direction_mask=0x%02x", cis_handle, direction_mask);

    BapServerRemoveIsoDataPathReq(bapUnicastServiceHandle, cis_handle, BAP_ISO_UNICAST, direction_mask);
}
